package utils;

import javax.servlet.ServletException;
import java.sql.*;

/**
 * <p>Title:util.DBConnection </p>
 */

//================================================
// This class creates a connection to database
//================================================

public class DBConnection {
  public static final boolean AutoCommiteOn = true;
  public static final boolean AutoCommiteOff = false;

  // connection parameters
  private String driverName = "com.mysql.jdbc.Driver";
  private String JDBCName = "jdbc:mysql";

  private Connection connection;
  private String tableName;
  private boolean autoCommite;

//================================================
// return the connection
//================================================

  public Connection getConnection() throws ServletException {
    Connect();
    return this.connection;
  }

  public Connection getConnection(String encoding) throws ServletException {
    Connect(encoding);
    return this.connection;
  }

//================================================
// constructors
//================================================

  public DBConnection() throws ServletException {
    this.autoCommite = AutoCommiteOn;
    this.tableName = DBVar.TABLENAME;
  }

  public DBConnection(boolean autoCommite) {
    this.autoCommite = autoCommite;
    this.tableName = DBVar.TABLENAME;
  }

  public DBConnection(String tableName, boolean autoCommite) {
    this.tableName = tableName;
    this.autoCommite = autoCommite;
  }
//================================================
// create connection to database
//================================================

  private void Connect() throws ServletException {
    try {
      //load MYSQL JDBC driver
      Class.forName(driverName).newInstance();

      String connectString = JDBCName + "://" + DBVar.HOST + "/" + tableName
              + "?user=" + DBVar.USER
              + "&password=" + DBVar.PASSWORD
              + "&autoReconnect=true&useUnicode=true&characterEncoding=UTF-8";

      // connect to Mysql server
      connection = DriverManager.getConnection(connectString);
      connection.setAutoCommit(autoCommite);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new ServletException(e.toString());
    }
  }

  private void Connect(String targetEncoding) throws ServletException {
    try {
      //load MYSQL JDBC driver
      Class.forName(driverName).newInstance();

      String connectString = JDBCName + "://" + DBVar.HOST + "/" + tableName
              + "?user=" + DBVar.USER
              + "&password=" + DBVar.PASSWORD
              + "&autoReconnect=true&useUnicode=true&characterEncoding=" + targetEncoding;

      // connect to Mysql server
      connection = DriverManager.getConnection(connectString);
      connection.setAutoCommit(autoCommite);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new ServletException(e.toString());
    }
  }

  public static int getLastID(Connection con) throws SQLException {
    int num = -1;
    String sql = "SELECT LAST_INSERT_ID()";
    Statement sta = con.createStatement();
    ResultSet res = sta.executeQuery(sql);

    if (res.next())
      num = res.getInt(1);

    res.close();
    sta.close();
    return num;
  }

  public static void close(Connection con) throws ServletException {
    try {
      con.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
